Skip to content

fix(DateType.cast): preserve null/undefined values instead of casting to unix timestamp#804

Merged
ejscribner merged 1 commit into
couchbaselabs:masterfrom
elmehdimrh:fix-date-cast-null
Oct 20, 2025
Merged

fix(DateType.cast): preserve null/undefined values instead of casting to unix timestamp#804
ejscribner merged 1 commit into
couchbaselabs:masterfrom
elmehdimrh:fix-date-cast-null

Conversation

@elmehdimrh
Copy link
Copy Markdown
Contributor

Problem

Currently, when a schema field is defined as Date and the stored database value of this field is null, DateType Class casts the value a JavaScript Date object representing the Unix epoch (1970-01-01T00:00:00Z).

This behavior causes unintended side effects:

  • null values are misrepresented as a valid date.
  • Application logic may misinterpret 1970-01-01 as meaningful rather than "no value".

Example:

const doc = await Model.findById(someId);
console.log(doc.dateField); 
// Current behavior → Date("1970-01-01T01:00:00.000Z")
// Expected behavior → null

Proposed Change

Update the DateType.cast() method to return null or undefined as-is, instead of coercing to a Date.

Benefits

  • Data integrity: Keeps null values intact, preserving the intent of the stored data.
  • Predictability: Matches developer expectations when dealing with optional date fields.
  • Backward-compatible: Existing behavior for valid/invalid values remains unchanged.

@ejscribner ejscribner closed this Oct 20, 2025
@ejscribner ejscribner reopened this Oct 20, 2025
@ejscribner ejscribner merged commit 9c2b7b7 into couchbaselabs:master Oct 20, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants